package com.cloudlinkscm.base.filecenter.advice;

import com.cloudlinkscm.base.filecenter.model.dto.RequestAuthenticationDto;
import com.cloudlinkscm.base.filecenter.service.RequestAuthenticationService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 文件中心api请求校验切面
 *
 * @author : tac
 * @date : 2017/7/21
 */
@Aspect
@Component
public class RequestAuthenticationAspect {

    @Autowired
    private RequestAuthenticationService authenticationService;

    @Pointcut("execution(* com.cloudlinkscm.base.filecenter.controller.FileController.*(..))")
    public void authenticationPointcut(){}

    @Before("authenticationPointcut()")
    public void beforeAuthenticate(JoinPoint joinPoint){
        RequestAuthenticationDto dto = null;
        for (Object arg: joinPoint.getArgs()){
            if(RequestAuthenticationDto.class.isAssignableFrom(arg.getClass())){
                dto = (RequestAuthenticationDto) arg;
                break;
            }
        }
        if (dto != null) {
            authenticationService.verifyValidity(dto);
            authenticationService.doAuthenticate(dto);
        }
    }
}
